package com.yc.txx.servlet;

import com.google.gson.Gson;
import com.yc.txx.entity.Car;
import com.yc.txx.mapper.CarMapper;
import com.yc.txx.util.MyBatisHelper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "FindCarServlet", value = "/FindCarServlet.s")
public class FindCarServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try( SqlSession session = MyBatisHelper.getSession();){
            final CarMapper mapper = session.getMapper(CarMapper.class);
            final Car car = mapper.selectById(request.getParameter("id"));
            final String json = new Gson().toJson(car);
            response.getWriter().append(json);
        }
    }
}
